<?php
/**
 * Created by PhpStorm.
 * User: 76683
 * Date: 2018/7/22
 * Time: 14:38
 */

namespace app\api\model;


use think\Model;

class WeUsers extends Model
{
    protected $autoWriteTimestamp = true;

    /**
     * 微信用户登录
     * @param $data
     * @return array
     * @throws \think\Exception
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function saveUser($data)
    {
        //判断用户存在不
        $exit = self::where('open_id', '=', $data['open_id'])->whereOr('union_id', '=', $data['union_id'])->field('id,nick_name,gender,change_name,nick_name,we_nick_name,reset_name')->fetchSql(false)->find();
        if (empty($exit)) {
            //新用户 注册
            $data['nick_name'] = $data['nickName'];
            $data['avatar_url'] = $data['avatarUrl'];
            $data['union_id'] = $data['union_id'] ? $data['union_id'] : '0';
            $data['status'] = 1;
            $data['is_delete'] = 0;
            $weUsers = new WeUsers();
            $data['we_nick_name'] = $weUsers->genWeNickName();
            $data['reset_name'] = $data['we_nick_name'] ;
            $res = self::create($data);
            return empty($res) ? [] : $res->toArray();
        } else {
            //老用户 返回
            return $exit->toArray();
        }
    }

    /**
     * 获取用户信息
     * @param $id
     * @return array
     * @throws \think\Exception
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function getUserInfo($id)
    {
        $info = self::where(['id' => $id])->find();
        if (empty($info)) {
            return ['code' => 0, 'msg' => '没有查询到用户信息'];
        } else {
            return $info->toArray();
        }
    }

    public function changeNickName($nick_name, $uid)
    {
        $userInfo = $this->getUserInfo($uid);
        $validUser = $this->validUser($userInfo);
        if (!$validUser){
            return ['code' => 0, 'msg' => '用户信息失效'];
        }
     //判断是否已经修改过
        if ($userInfo['change_name'] == 1){
            return ['code' => 0, 'msg' => '您已经修改过用户名了'];
        }
        $res = self::where(['id'=>$uid])->update(['we_nick_name'=>$nick_name,'change_name'=>1]);
        if($res){
            return ['code' => 1, 'msg' => '改名成功'];
        }else{
            return ['code' => 0, 'msg' => '改名失败'];
        }
    }

    /**
     * 判断用户是否有效
     * @param $userInfo
     * @return bool
     */
    public function validUser($userInfo)
    {
        $valid = (!isset($userInfo['status']) or !isset($userInfo['is_delete'])) ? false : true;
        if ($valid) {
            $valid = ($userInfo['status'] == 0 or $userInfo['is_delete'] == 1) ? false : true;
        }
        return $valid;
    }

    /**
    *获取we_nick_name
    *
    *return string
    *
    */
    public function genWeNickName(){
        $max_id = self::max('id');
        $max_id = rand(0,9).$max_id;
        $len = strlen($max_id);
        if($len < 6){
            $max_id = str_pad($max_id,'0',STR_PAD_LEFT);
        }else{
            $max_id += 1;
        }
        return '用户'.$max_id;
    }
}